package org.judo.classlocator;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.judo.properties.Props;

public class ClassLocator
{
	public static Class findClassInProject(String name,String startingPackage)
		throws ClassNotFoundException
	{
		try
		{
			String modelPath = (String)Props.get("judo.models.base.classpath");
			Class modelClass = Class.forName(modelPath+"."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		try
		{
			String controllerPath = (String)Props.get("judo.controllers.base.classpath");
			Class modelClass = Class.forName(controllerPath+"."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		try
		{
			Class modelClass = Class.forName(startingPackage+"."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		try
		{
			Class modelClass = Class.forName("org.judo.admin."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		try
		{
			Class modelClass = Class.forName("org.judo.admin.identity."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		try
		{
			Class modelClass = Class.forName(startingPackage+"."+name.toLowerCase()+"."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		try
		{
			Class modelClass = Class.forName(startingPackage+"."+name.toLowerCase().replace("_","")+"."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		try
		{
			String modelPath = (String)Props.get("judo.models.base.classpath");
			Class modelClass = Class.forName(modelPath+"."+name.toLowerCase()+"."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		try
		{
			String modelPath = (String)Props.get("judo.models.base.classpath");
			Class modelClass = Class.forName(modelPath+"."+name.toLowerCase().replace("_","")+"."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		try
		{
			Class modelClass = Class.forName(startingPackage+"."+name);
			return modelClass;
		}
		catch(ClassNotFoundException e){
			//ignore
		}
		
		throw new ClassNotFoundException(name);
	}
}
